﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Data.OleDb;
using Com.Toook.Common;

public partial class nst_article_ArticleAdd : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            InitDropDownListType();
        }
    }
    protected void ButtonAdd_Click(object sender, EventArgs e)
    {
        LabelMsg.Text = "";
        if (!Page.IsValid)
        {
            LabelMsg.Text = "输入内容不合法";
            return;
        }
        int totalPage = 1;
        int remander = 0;
        int length = txtContent.Text.Length;
        int pageLength = 3000; //每页多少字
        int minRemainder = 300; //最后一页小于此值时附加到前一页，不单独作为一页
        if (length > pageLength) //长度超过 pageLength，分页
        {
            totalPage = length / pageLength; //得到整页
            remander = length % pageLength; //得到剩余文字长度
            if (remander > minRemainder)//如果最后一页剩余文字长度大于300字，放在单独一页。小于300字，合并到最后一页。
            {
                totalPage++;
            }
        }
        int typePageId = PageData.GetNextTypePageId(int.Parse(ddlType.SelectedValue));
        string sqlArticle = "insert into PublishArticle(title, typePageId, author, sourceSite, totalPage, content, score) " +
            " values(@title, @typePageId, @author, @sourceSite, @totalPage, @content, @score) ";
        OleDbConnection con = AccessHelper.GetConnection();
        OleDbCommand cmd = new OleDbCommand(sqlArticle, con);
        cmd.Parameters.Add("@title", OleDbType.VarWChar, 30).Value = txtTitle.Text;
        cmd.Parameters.Add("@typePageId", OleDbType.Integer).Value = typePageId;        
        cmd.Parameters.Add("@author", OleDbType.VarWChar, 50).Value = txtAuthor.Text;
        cmd.Parameters.Add("@sourceSite", OleDbType.VarWChar, 50).Value = txtSourceSite.Text;        
        cmd.Parameters.Add("@totalPage", OleDbType.Integer).Value = totalPage;

        try
        {
            con.Open();
            if (totalPage == 1)
            {                
                cmd.Parameters.Add("@content", OleDbType.LongVarWChar).Value = txtContent.Text;
                //score只有PublishArticle表有，不能在上面添加。否则后面的参数会错位。
                //Access现在虽然可以使用@name形式的参数，但是实质和？占位符一样，必须按顺序设置参数。不能像SqlServer一样根据@name设置参数。
                cmd.Parameters.Add("@score", OleDbType.Integer).Value = ddlScore.SelectedValue;
                cmd.ExecuteNonQuery();
            }
            else
            {
                cmd.Parameters.Add("@content", OleDbType.LongVarWChar).Value = txtContent.Text.Substring(0, pageLength);
                OleDbParameter scoreParam = new OleDbParameter("@score", ddlScore.SelectedValue);
                cmd.Parameters.Add(scoreParam);
                cmd.Parameters.Add("@pageNum", OleDbType.Integer).Value = 1;//为PublishArticleMorePage的pageNum添加占位符
                cmd.ExecuteNonQuery();//将第一页数据插入到 PublishArticle 表
                cmd.Parameters.Remove(scoreParam);//删除score参数

                //剩下的数据插入到PublishArticleMorePage
                string sqlArticleMorePage = "insert into PublishArticleMorePage(title, typePageId, author, sourceSite, totalPage, content, pageNum) " +
                    " values(@title, @typePageId, @author, @sourceSite, @totalPage, @content, @pageNum) ";
                cmd.CommandText = sqlArticleMorePage;//重新设置插入语句

                for (int i = 2; i < totalPage; i++)
                {

                    cmd.Parameters["@content"].Value = txtContent.Text.Substring((i - 1) * pageLength, pageLength);
                    cmd.Parameters["@pageNum"].Value = i;
                    cmd.ExecuteNonQuery();
                }
                //插入最后一页。有可能大于pageLength，所以直接到结尾
                cmd.Parameters["@content"].Value = txtContent.Text.Substring((totalPage - 1) * pageLength);
                cmd.Parameters["@pageNum"].Value = totalPage;
                cmd.ExecuteNonQuery();
            }
            //插入成功后更新typeTotalCount
            PageData.UpdateTypeTotalCount(ddlType.SelectedValue);
            PageData.RefreshPageParam();
            LabelMsg.Text = "添加成功：" + txtTitle.Text;
            txtTitle.Text = txtContent.Text = txtAuthor.Text = txtSourceSite.Text = "";
        }
        catch (Exception ex)
        {
            LabelMsg.Text = "添加失败" + ex.Message;
            LogHelper.Log.Error("添加article失败。", ex);
        }
        finally
        {
            con.Close();
        }
    }
    private void InitDropDownListType()
    {
        DataRow[] rows = PageData.SelectParamRows("parentId=60");
        foreach (DataRow row in rows)
        {
            ddlType.Items.Add(new ListItem(row["typeName"].ToString(), row["typeId"].ToString()));
        }
    }
}
